package com.qxsx.midi.activity.exam;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.GridView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.ClassVideoAdapter;
import com.qxsx.midi.model.ClassVideo;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.PullToRefresh.PullToRefreshBase;
import com.qxsx.midi.view.PullToRefresh.PullToRefreshGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zrj on 2015/9/7.
 */
public class ClassVideoListActivity extends BaseActivity {
    private CustomProgress customProgress;

    private PullToRefreshGridView lv_classvideo;
    private List<ClassVideo> list;
    private int pageNum = 1;//初始化页号为1
    private ClassVideoAdapter classVideoAdapter;

    //执行下拉、上拉线程
    private Handler mHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exma_classvideo_list);
        setTitle("公开课视频");
        setNavBtn(R.mipmap.back, 0);


        findById();
        InitData();


        mHandler = new Handler();


    }


    private void classvideo(int pageNum) {
        customProgress = CustomProgress.show(ClassVideoListActivity.this, "请稍后....", true, null);
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("nowpage", pageNum + "");
            object.put("pagesize", "10");

            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("511", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                }
                try {
                    list = new ArrayList<ClassVideo>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        ClassVideo classVideo = new ClassVideo();
                        classVideo.setClv_name(object1.getString("lesson_name"));
                        classVideo.setClv_content(object1.getString("lesson_brief"));
                        classVideo.setClv_id(object1.getString("id"));
                        classVideo.setClv_img(object1.getString("lesson_icon"));
                        //Log.w("lesson_icon", object1.getString("lesson_icon"));
                        list.add(classVideo);
                    }
                    classVideoAdapter.removeAll();
                    classVideoAdapter.addData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                customProgress.dismiss();
            }
        });


    }

    private void InitData() {
        list = new ArrayList<>();

        classVideoAdapter = new ClassVideoAdapter(ClassVideoListActivity.this, list);
        lv_classvideo.setAdapter(classVideoAdapter);
        lv_classvideo.setMode(PullToRefreshBase.Mode.BOTH);
        lv_classvideo.setOnRefreshListener(new LvOnRefreshListener(lv_classvideo));

        classvideo(1);
    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    private void findById() {
        lv_classvideo = (PullToRefreshGridView) findViewById(R.id.lv_classvideo);


    }

    /**
     * 上啦加载
     */
    private void LoadMore() {
        pageNum++;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("nowpage", pageNum + "");
            object.put("pagesize", "10");

            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("511", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                }
                try {
                    list = new ArrayList<ClassVideo>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        ClassVideo classVideo = new ClassVideo();
                        classVideo.setClv_name(object1.getString("lesson_name"));
                        classVideo.setClv_content(object1.getString("lesson_brief"));
                        classVideo.setClv_id(object1.getString("id"));
                        classVideo.setClv_img(object1.getString("lesson_icon"));
                        //Log.w("lesson_icon", object1.getString("lesson_icon"));
                        list.add(classVideo);
                    }
                    classVideoAdapter.addData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


    }

    /**
     * 下拉刷新
     */
    private void LoadRefresh() {
        pageNum = 1;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("nowpage", 1 + "");
            object.put("pagesize", "10");

            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("511", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                }
                try {
                    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
                    list = new ArrayList<ClassVideo>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        ClassVideo classVideo = new ClassVideo();
                        classVideo.setClv_name(object1.getString("lesson_name"));
                        classVideo.setClv_content(object1.getString("lesson_brief"));
                        classVideo.setClv_id(object1.getString("id"));
                        classVideo.setClv_img(object1.getString("lesson_icon"));
                        //Log.w("lesson_icon", object1.getString("lesson_icon"));
                        list.add(classVideo);
                    }
                    classVideoAdapter.removeAll();
                    classVideoAdapter.addData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });
    }

    class LvOnRefreshListener implements PullToRefreshBase.OnRefreshListener2<GridView> {

        private PullToRefreshGridView mPtflv;

        public LvOnRefreshListener(PullToRefreshGridView ptflv) {
            this.mPtflv = ptflv;
        }

        @Override
        public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
            new GetDownTask(mPtflv).execute();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
            new GetUpTask(mPtflv).execute();
        }
    }

    class GetDownTask extends AsyncTask<String, Void, Integer> {
        private PullToRefreshGridView mPtrlv;

        public GetDownTask() {
        }

        public GetDownTask(PullToRefreshGridView ptrlv) {
            this.mPtrlv = ptrlv;

        }

        @Override
        protected Integer doInBackground(String... params) {

            return null;
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            LoadRefresh();
            mPtrlv.onRefreshComplete();
        }

    }

    class GetUpTask extends AsyncTask<String, Void, Integer> {
        private PullToRefreshGridView mPtrlv;
        public String content = null;

        public GetUpTask() {
        }

        public GetUpTask(PullToRefreshGridView ptrlv) {
            this.mPtrlv = ptrlv;

        }

        @Override
        protected Integer doInBackground(String... params) {

            return null;
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            LoadMore();
            mPtrlv.onRefreshComplete();
        }

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }

    }
}
